package com.heima.article.listener;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class UpDownListener {

    @Autowired
    private IApArticleService iApArticleService;

    @KafkaListener(topics="${topic.upDownTopic}")
    public void handleMessage(ConsumerRecord<String,String> record){
        String value = record.value();
        if (StringUtils.isNotEmpty(value)){
            HashMap map = JSON.parseObject(value, HashMap.class);
            Long id = (Long) map.get("id");
            Boolean isDown = (Boolean) map.get("isDown");

            LambdaUpdateWrapper<ApArticle> update = new LambdaUpdateWrapper<>();
            update.eq(ApArticle::getId,id);
            update.set(ApArticle::getIsDown,isDown);
            iApArticleService.update(update);
        }
    }
}
